//
//  ZhuCeViewController.m
//  KCFurProject
//
//  Created by ios程序员 on 15/11/6.
//  Copyright (c) 2015年 ios程序员. All rights reserved.
//

#import "ZhuCeViewController.h"
#import <MessageUI/MessageUI.h>


@interface ZhuCeViewController ()<MFMessageComposeViewControllerDelegate>

@property (nonatomic ,strong) UILabel *zhuCeLabel;


@end

@implementation ZhuCeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
#pragma mark - 系统发送短信
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(120, 100, 200, 30);
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    button.backgroundColor = [UIColor cyanColor];
    [button setTitle:@"方法1:短信" forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
#pragma mark - 发送后能退出回到APP
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(120, 200, 200, 30);
    [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

//    button1.backgroundColor = [UIColor cyanColor];
    [button1 setTitle:@"点击获取验证码" forState:UIControlStateNormal];
    [self.view addSubview:button1];
    [button1 addTarget:self action:@selector(button1Clicked:) forControlEvents:UIControlEventTouchUpInside];

    
}

/**
 *  系统发送短信
 *
 *  @param button nil
 */
- (void)buttonClicked:(UIButton *)button
{
    NSURL *url = [NSURL URLWithString:@"sms://10086"];
    [[UIApplication sharedApplication]openURL:url];
    
}

/**
 *  发送后能回到APP
 *
 *  @param button nil
 */
- (void)button1Clicked:(UIButton *)button
{
    
    if ([MFMessageComposeViewController canSendText]) {
        MFMessageComposeViewController *MFMC = [[MFMessageComposeViewController alloc]init];
        MFMC.messageComposeDelegate = self;
        MFMC.body = @"YOU";
        MFMC.recipients = [NSArray arrayWithObject:@"15124609600"];
        
#pragma mark - 添加图片(可以发彩信)
        //            /**
        //             *  添加附件(图片为例)
        //             */
        //            UIImage *addPic = [UIImage imageNamed: @"1.png"];
        //            NSData *imageData = UIImagePNGRepresentation(addPic);            // png或者jpg
        //            [MFMC addAttachmentData:imageData typeIdentifier:@"image/png" filename:@"1.png"];
        
        [self presentViewController:MFMC animated:YES completion:^{
            
        }];
    }
    else
    {
//        UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"提示" message:@"没找到" preferredStyle:UIAlertControllerStyleActionSheet];
//        
//        [controller actions];
        
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"该设备不支持短信功能" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
        [alert show];
    }
    
}
//15124609600
#pragma mark - 协议方法
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    NSString *msg;
    switch (result) {
        case MessageComposeResultCancelled:
            
            msg = @"发送取消";
            [self alertWithTitle:nil msg:msg];
            break;
        case MessageComposeResultFailed:
            
            msg = @"发送取消";
            [self alertWithTitle:nil msg:msg];
            break;
        case MessageComposeResultSent:
            
            msg = @"发送成功";
            [self alertWithTitle:nil msg:msg];
            break;
            
        default:
            break;
    }
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

/**
 *  提示框
 *
 *  @param title title(nil)
 *  @param msg   提示信息
 */
- (void) alertWithTitle: (NSString *)title msg: (NSString *)msg
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:msg
                                                   delegate:nil
                                          cancelButtonTitle:@"确定"
                                          otherButtonTitles:nil];
    [alert show];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
